





function NewlyAdded() {
    var jiaDrugNum = $(".jiaDrugNum").val();//药品名称
    var stoPcs = $("#takestoFrom [name=stoPcs]").val();//数量
    var stoLotnum=$("#stoLotnum").val();//批号
    $(".jiaDrugNum").change(function () {
        $(".checkSel").html("");
    });
    $("#takestoFrom [name=stoPcs]").change(function () {
        $(".checkSel2").html("")
    })
    if(jiaDrugNum==-1){
        $(".checkSel").html("请选择药品名称!")
        return;
    }
    if(stoPcs==null || stoPcs==''){
        $(".checkSel2").html("请输入药品数量!")
        return;
    }else if(stoPcs<0){
        $(".checkSel2").html("请输入正确数量!")
        return;
    }
    var drugNames = $("#info .drugNums");
    var drugNames2 = $("#info .drugNums").html();
    var stoLotnums = $("#info .stoLotnums");
    if(drugNames.length>0){
        for(var i = 0;i<drugNames.length;i++){
            if(drugNames[i].innerHTML==jiaDrugNum){
                for (var y = 0; y < stoLotnums.length; y ++){
                    if(stoLotnums[i].innerHTML==stoLotnum&&drugNames[i].innerHTML==jiaDrugNum){
                        swal({
                            title:"新增提示",
                            text:"该信息已经增加过了？",
                            type:"warning"
                        })
                        return;
                    }
                }
            }

        }
    }
    $.post(ctx+"/stock/addSessionMap","drugNum="+jiaDrugNum+"&stoPcs="+stoPcs+"&stoLotnum="+stoLotnum,function (keyId) {
        $.post(ctx+"/stock/searchDruginfoByNum","drugNum="+jiaDrugNum,function (drug) {
            $("<tr>"+
                "<td class='drugNums'>"+drug.drugNum+"</td>"+
                "<td class='drugName'>"+drug.drugName+"" +
                "<input type='hidden' value='"+keyId+"' name='keyId'/></td>"+
                "<td>"+drug.drugStandard+"</td>"+
                "<td class='stoLotnums'>"+stoLotnum+"</td>"+
                "<td>"+drug.drugPreparation+"</td>"+
                "<td>"+stoPcs+"</td>"+
                "<td><button type='button'' onclick='delDels(this)'  class='layui-btn layui-btn-danger layui-btn-mini del'>删除</button></td>"+
                "</tr>").appendTo("#info");

        },'json');
    },'text')
}
//删除
function delDels(event) {
    var i = $(event).parent().siblings(".drugName").children("").val();
    swal({
        title:"确认提示",
        text:"确认移除此条信息?",
        type:"warning",
        showCancelButton:true,
        closeOnConfirm:false,
        confirmButtonText:"删除",
        confirmButtonColor:"#DD6B55",
        cancelButtonText: "取消"
    }).then(function () {
        $.post(ctx+"/stock/removeDrugstockSession","keyId="+i,function (msg) {
            toastr.info(msg);
            $(event).parent().parent().remove();
        })
    })

}

//模态框
function addTakesto() {
    $(".modal-header").css("background","#CCCCCC");
    $(".modal-title").css("font-size","18px");
    $(".modal-dialog").css({"width":"75%"})
    Reset();//调用重置的方法!
    $("#TakestoAdd").dialog("open");
    $("#TakestoAdd").css("display","block")
}


//药品改变批号
function changeDrug(v) {
    $("#stoLotnum").children().remove();//刷新一次删除一次
    $.post(ctx+"/stock/searchstoLotnum","drugNum="+v,function (arr) {
        $(arr).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.stoLotnum);
            $opt.html(this.stoLotnum);
            $opt.appendTo("#stoLotnum");
        });
    },'json')
}

//初始化药品下拉列表
function initDrugSel(){
    $(".jiaDrugNum").children().not(".bus").remove();
    $.ajax({
        url:ctx+"/pur/searchDrug",
        dataType:"json",
        async:false,
        success:function(drug){
            $(drug).each(function(){
                var $opt = $("<option/>");
                $opt.val(this.drugNum);
                $opt.html(this.drugName);
                $opt.appendTo(".jiaDrugNum");
            });
        }
    });
}
//初始化模态框
function inittakestoAdd() {
    $("#TakestoAdd").dialog({
        title:"添加库存盘点信息",
        btnValue:"确定",
        addBind:function () {
            $("#TakestoAdd").dialog("close")
            refresh();
        }
    })
}
function refresh() {
    $.get(ctx+"/stock/searchSessionMap",null,function (arr) {
        $(".tbody").children("").remove();
        for(var key in arr){
            $("<tr class='takestoH'>" +
                "<td class='drugName'>" +
                "<input type='hidden' value='"+key+"' name='keyId' />"+arr[key].druginfo.drugName+"</td>" +
                "<td>"+arr[key].stoLotnum+"</td>" +
                "<td>"+arr[key].stoPcs+"</td>" +
                "<td>"+arr[key].druginfo.drugtype.typeName+"</td>" +
                "<td>"+arr[key].stoPresell+"</td>" +
                "<td>"+arr[key].stoPresell*arr[key].stoPcs+"</td>" +
                "<td><button class='layui-btn layui-btn-normal layui-btn-mini updateDrug' type='button' onclick='updateDetails(this)'>编辑</button></td>" +
                "</tr>").appendTo(".tbody")
        }
    },'json')
}
$(function () {
    inittakestoAdd();//添加模态框
    initDrugSel();
    initUpdateModal();//修改模态框
    initSelect2();//下拉框
});
//select2
$(function () {
    $('.jiaDrugNum').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });
    $('#invPers').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });
    $('#invAuditper').select2({
        placeholder: "请选择",
        language: "zh-CN"
    });
})

function updateDetails(event) {
    var keyId = $(event).parent().siblings(".drugName").children("").val();//获取要进行修改的KeyId
    $("#updateForm [name=keyId]").val(keyId)//session里面的KeyId赋值
    $("#updateForm [name=stoLotnum]").children().remove();//刷新一次删除一次
    $("#updateForm").dialog("open");
    $(".modal-header").css("background","#ffffff");
    $(".modal-title").css("font-size","14px");
    $(".modal-dialog").css({"width":""})
    $("#updateForm").css("display","block");
    $.post(ctx+"/stock/searchSessionByKeyId","keyId="+keyId,function (drugstock) {
        $("#updateForm [name=drugName]").val(drugstock.druginfo.drugName);
        $.post(ctx+"/stock/searchstoLotnum","drugNum="+drugstock.drugNum,function (arr) {
            $(arr).each(function(){
                var $opt = $("<option/>");
                $opt.val(this.stoLotnum);
                $opt.html(this.stoLotnum);
                $opt.appendTo("#updateForm [name=stoLotnum]");
            });
        },'json')
        $("#updateForm [name=stoPcs]").val(drugstock.stoPcs)
    },'json')
}
//修改模态框
function initUpdateModal() {
    $("#updateForm").dialog({
        title:"修改库存信息",
        btnValues:"确定",
        addBind:function () {
            var param = $("#serForm").serialize();
            $.post(ctx+"/stock/updateSessionDrugstock",param,function (msg) {
                $("#updateForm").dialog("close");
                toastr.info(msg)
                refresh();
            })
        }
    })
}

//删除session
$.post(ctx+"/stock/deleteSession",null,function (msg) {

})
//重置按钮
function Reset() {

    var jiaDrugNum = $(".jiaDrugNum").select2();
    jiaDrugNum.val(-1).trigger("change");
    var stoPcs = $("#takestoFrom [name=stoPcs]").val("");//数量
    var stoLotnum=$("#stoLotnum").val("");//批号
}
function initSelect2() {
    $.post(ctx+"/stock/searchAllWorker",null,function (arr) {
        $(arr).each(function () {
            var $opt = $("<option/>")
            $opt.html(this.woName)
            $opt.val(this.woJubnum)
            $opt.appendTo("#invPers")
        })
    },'json');
    $.post(ctx+"/stock/searchAllWorker",null,function (arr) {
        $(arr).each(function () {
            var $opt = $("<option/>")
            $opt.html(this.woName)
            $opt.val(this.woJubnum)
            $opt.appendTo("#invAuditper")
        })
    },'json')
}
$(function () {
    laydate.render({
        elem:'[name=invDate]'
    })
})
$(function () {
    $("#invPers").select2();
})
function Submission() {
    var invPers= $("#SubmissionForm [name=invPers]").val();//盘点人员
    var invDate = $("#SubmissionForm [name=invDate]").val();//盘点时间
    var invAuditper = $("#SubmissionForm [name=invAuditper]").val();//审核人员
    $("#SubmissionForm [name=invPers]").change(function () {
        $(".invPersError").html("")
    })
    if(invDate!=null || invDate!=''){
        $("#SubmissionForm [name=invDate]").siblings("span").html("")
    }
    $("#SubmissionForm [name=invAuditper]").change(function () {
        $(".invAditperError").html("");
    })
    if(invPers==null || invPers==''){
        $(".invPersError").html("请选择盘点人员!")
        return;
    }else if(invDate==null || invDate==''){
        $("#SubmissionForm [name=invDate]").siblings("span").html("请输入盘点时间!")
        return;
    }else if(invAuditper==-1){
        $(".invAditperError").html("请选择审核人员!")
        return;
    }else if($(".tbody .takestoH").html()==null){
        $("#tijiao").html("请输入您要盘点的物品!");
        return;
    }else{
        $("#SubmissionForm").submit();
    }

}